﻿using System;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Data;
using System.Drawing;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Diagnostics;

using RHFrameWork;

namespace ResumeHelper
{
    public class WebHelper
    {

        /// <summary>
        /// 发送post请求
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <param name="postString">发送到数据 例如:"name=xhan&password=1231"</param>
        /// <param name="encoding">发送和接受数据使用的编码</param>
        /// <returns>服务器响应字符串</returns>
        public static string SendPostRequest(string url, string postString)
        {
            Encoding encoding = Encoding.UTF8;

            byte[] postData = encoding.GetBytes(postString);

            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            client.Headers.Add("ContentLength", postData.Length.ToString());

            byte[] responseData = client.UploadData(url, "POST", postData);
            return encoding.GetString(responseData);
        }

        public static string getMailServer(string strEmail)
        {
            string strDomain = strEmail.Split('@')[1];
            ProcessStartInfo info = new ProcessStartInfo();
            info.UseShellExecute = false;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.FileName = "nslookup";
            info.CreateNoWindow = true;
            info.Arguments = "-type=mx " + strDomain;
            Process ns = Process.Start(info);
            StreamReader sout = ns.StandardOutput;
            Regex reg = new Regex("mail exchanger = (?<mailServer>[^\\s]+)");
            string strResponse = "";
            while ((strResponse = sout.ReadLine()) != null)
            {
                Match amatch = reg.Match(strResponse);
                if (reg.Match(strResponse).Success) return amatch.Groups["mailServer"].Value;

            }
            return null;
        }

        public static int checkEmail(string mailAddress)
        {

            Regex reg = new Regex("^[a-zA-Z0-9_-]+@([a-zA-Z0-9-]+\\.){1,}(com|net|edu|miz|biz|cn|cc)$");
            if (!reg.IsMatch(mailAddress))
                return 405;//Email地址形式上就不对

            string mailServer = getMailServer(mailAddress);
            if (mailServer == null)
            {
                return 404; //邮件服务器探测错误
            }
            TcpClient tcpc = new TcpClient();
            tcpc.NoDelay = true;
            tcpc.ReceiveTimeout = 3000;
            tcpc.SendTimeout = 3000;
            try
            {
                tcpc.Connect(mailServer, 25);
                NetworkStream s = tcpc.GetStream();
                StreamReader sr = new StreamReader(s, Encoding.Default);
                StreamWriter sw = new StreamWriter(s, Encoding.Default);
                string strResponse = "";
                string strTestFrom = "brookes_luan@yahoo.com.cn";
                sw.WriteLine("helo " + mailServer);
                sw.WriteLine("mail from:<" + mailAddress + ">");
                sw.WriteLine("rcpt to:<" + strTestFrom + ">");
                strResponse = sr.ReadLine();
                if (!strResponse.StartsWith("2")) return 403; //用户名有误
                sw.WriteLine("quit");
                return 200; //Email地址检查无误

            }
            catch
            {
                return 403;//发生错误或邮件服务器不可达
            }
        }
    }
}
